//
//  RCRestaurantRecommendVC.m
//  RocketProject
//
//  Created by chen jiedan on 13-6-26.
//  Copyright (c) 2013年 Rocket. All rights reserved.
//

#import "RCRestaurantRecommendVC.h"
#import "IDataModel.h"
#import "RCMerchantsListDataModel.h"
#import "IManager+FoodRecommend.h"
#import "RCMerchantListCell.h"
#import "RCMerchantDetailVC.h"
#import "CommonDefines.h"
#import "GloableEngineService.h"
#import "NSString+NSString_Ex_.h"
#import "UIColor-Expanded.h"

#define kBottomImageFile           @"image_merchant_street.png"

@interface RCRestaurantRecommendVC()<IManagerDelegate>{
    BOOL                     isRefresh;
    NSInteger                _pn;
    int64_t                  _requestID;
}

@property (nonatomic, copy) NSString *title;
@property (nonatomic, retain) RCRecommendRestaurantListDataModel *dataModel;
@end

@implementation RCRestaurantRecommendVC

@synthesize dataModel = _dataModel;
@synthesize title;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (id) initWithTitle:(NSString *)foodtitle
{
    self = [super init];
    if (self) {
        self.title = foodtitle;
    }
    return self;
}

- (void) dealloc
{
    self.dataModel = nil;
    self.title = nil;
    [super dealloc];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    
    [self refreshData];
    self.titleLabelText = @"美食推荐";
    self.navigationItem.titleView = [self createTitleView];
    

    // 设置tableview相关参数
    if ( baseVCTableView != nil ) {
        self.view.autoresizingMask |= UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleBottomMargin;
        baseVCTableView.autoresizingMask |= UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleBottomMargin;
        baseVCTableView.backgroundColor = [UIColor whiteColor];
        baseVCTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        baseVCTableView.frame = self.view.bounds;
        CGRect tableViewNewFrame = CGRectMake(baseVCTableView.frame.origin.x,baseVCTableView.frame.origin.y,baseVCTableView.frame.size.width,UIScreenHeight-20);
        baseVCTableView.frame = tableViewNewFrame;
        baseVCTableView.tableHeaderView = nil;
        baseVCTableView.tableFooterView = nil;
        [self addPullRefresh];
    }
    
}


- (void) viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [[IManager sharedInstance] cancelFetchDataRequestByRequestID:_requestID];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark tableview
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.dataModel.listItems count];
}

- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}
- (UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellID = @"RCMerchantListCell";
    RCMerchantListCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        cell = [[[RCMerchantListCell alloc] init] autorelease];
    }
    [cell bindData:[self.dataModel.listItems objectAtIndex:[indexPath row]]];
    return cell;
}

- (void)  tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    RCMerchant *merchant = [self.dataModel.listItems objectAtIndex:[indexPath row]];
    RCMerchantDetailVC *detail = [[RCMerchantDetailVC alloc] initWithMerchantID:merchant.merchantID];
    [self.navigationController pushViewController:detail animated:YES];
    [detail release];
    
}

-(void) loadMoreData
{
    isRefresh = NO;
    NSString *page = [NSString stringWithFormat:@"%d",_pn];
    _requestID = [[IManager sharedInstance] requestRecommendRestaurantForFood:self.title forPage:page withDelegate:self];
    
}



#pragma
#pragma mark ServerData ----下拉刷新数据

-(void) refreshData
{
    isRefresh = YES;
    _pn = 0;
    _requestID = [[IManager sharedInstance] requestRecommendRestaurantForFood:self.title forPage:@"0" withDelegate:self];
}



#pragma
#pragma mark IManage Delegate
-(void) updateViewForSuccess:(IDataModel *)dataModel withRequestId:(int64_t)requestId
{
    
    if (isRefresh) {
        //下拉刷新
        self.dataModel = (RCRecommendRestaurantListDataModel *)dataModel;
        [self finishRefreshingData];
    }else{
        //加载更多的
        RCRecommendRestaurantListDataModel *newData = (RCRecommendRestaurantListDataModel *)dataModel;
        self.dataModel.pn = newData.pn;
        self.dataModel.rn = newData.rn;
        self.dataModel.total = newData.total;
        [self.dataModel.listItems addObjectsFromArray:newData.listItems];
        
        [baseVCTableView reloadData];
        [self finishLoadMoreData];
    }
    
    
    NSInteger currentTotal = [self.dataModel.listItems count];
    _pn = currentTotal;
    if (currentTotal < self.dataModel.total) {
        [self addPullLoadMore];
    }else{
        [self noMoreData];
    }
    
}

-(void) updateViewForError:(NSError *)errorInfo withRequestId:(int64_t)requestId
{
    
}


@end
